package com.aaa.order.controller;

import com.aaa.entity.Order;
import com.aaa.entity.Product;
import com.aaa.order.feign.ProductFeign;
import com.aaa.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

/**
 * @program: qy156-shop-parent
 * @description:
 * @author: 闫克起2
 * @create: 2022-11-17 16:33
 **/
@RestController
@RequestMapping("/order")
public class OrderController {




    @Autowired
    private IOrderService orderService;


    //spring容器会为该接口生成带来实现类。
    @Autowired
    private ProductFeign productFeign;

    @GetMapping("/saveOrder")
    public Order saveOrder(Long pid,Integer num){
        Order order=new Order();
        //用户得信息可以从登录后获取--Session  redis  jwt
        order.setUid(1);
        order.setUsername("张恒");
        //为订单对象设置商品得信息
        order.setPid(pid);
        Product product = productFeign.getById(pid);

        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(num);

        orderService.save(order);
        return order;
    }


    @GetMapping("getOrder/{oid}")
    public Order getOrder(@PathVariable Long oid){
       return orderService.findOrderById(oid);
    }

    @GetMapping("queryOrder")
    public String queryOrder(){
        orderService.queryGood();
        return "查询订单";
    }

    @GetMapping("addOrder")
    public String addorder(){
        orderService.queryGood();
        return "添加订单";
    }
}
